package com.mes.loco.aps.server.controller.wms;

import com.iplant.aps.controller.BaseController;
import com.iplant.aps.service.MyHelperService;
import com.iplant.aps.service.WMSService;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.bfc.BFCMessage;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.bpm.BPMTaskBase;
import com.iplant.aps.service.po.wms.MESMaterialReturn;
import com.iplant.aps.service.utils.CloneTool;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.utils.RetCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 产线工位退料需求控制器
 *
 * @author PengYouWang
 * 2022-3-25 09:11:48
 */
@RestController
@RequestMapping("/api/MESMaterialReturn")
public class MESMaterialReturnController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(MESMaterialReturnController.class);

    final
    WMSService wWMSService;

    final MyHelperService wMyHelperService;

    public MESMaterialReturnController(WMSService wWMSService, MyHelperService wMyHelperService) {
        this.wWMSService = wWMSService;
        this.wMyHelperService = wMyHelperService;
    }

    /**
     * 条件查询产线工位退料需求
     */
    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Object wResult;
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            String wAsnType = StringUtils.parseString(request.getParameter("AsnType"));
            String wRMaNO = StringUtils.parseString(request.getParameter("RMaNO"));
            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"));
            int wLineID = StringUtils.parseInt(request.getParameter("LineID"));
            int wCustomerID = StringUtils.parseInt(request.getParameter("CustomerID"));
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            int wUpFlowID = StringUtils.parseInt(request.getParameter("UpFlowID"));
            String wMaterial = StringUtils.parseString(request.getParameter("Material"));
            int wStatus = StringUtils.parseInt(request.getParameter("Status"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            ServiceResult<List<MESMaterialReturn>> wServiceResult = wWMSService.MES_QueryMaterialReturnList(wLoginUser,
                    wAsnType, wRMaNO, wProductID, wLineID, wCustomerID, wOrderID, wPartID, wStatus, wMaterial,
                    wStartTime, wEndTime, wUpFlowID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询退料需求单明细
     */
    @GetMapping("/Info")
    public Object Info(HttpServletRequest request) {
        Object wResult;
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wDemandID = StringUtils.parseInt(request.getParameter("DemandID"));

            if (wDemandID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<MESMaterialReturn> wServiceResult = wWMSService.MES_QueryMaterialReturn(wLoginUser,
                    wDemandID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 更新主单据
     */
    @PostMapping("/Update")
    public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult;
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            MESMaterialReturn wMESMaterialReturn = CloneTool.Clone(wParam.get("data"), MESMaterialReturn.class);

            ServiceResult<Integer> wServiceResult = wWMSService.WMS_UpdateMaterialReturn(wLoginUser,
                    wMESMaterialReturn);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 退料(模拟)
     */
    @GetMapping("/Back")
    public Object Back(HttpServletRequest request) {
        Object wResult;
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wDemandID = StringUtils.parseInt(request.getParameter("DemandID"));

            if (wDemandID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wWMSService.MES_BackMaterial(wLoginUser, wDemandID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询工序排程计划
     */
    @GetMapping("/EmployeeAll")
    public Object EmployeeAll(HttpServletRequest request) {
        Object wResult;
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            int wTagTypes = StringUtils.parseInt(request.getParameter("TagTypes"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            ServiceResult<List<BPMTaskBase>> wServiceResult = wWMSService.WMS_QueryMaterialReturnEmployeeAll(wLoginUser,
                    wTagTypes, wStartTime, wEndTime);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 获取待办、已办、发起 偶换件不合格评审任务
     */
    @GetMapping("/EmployeeAllWeb")
    public Object EmployeeAllWeb(HttpServletRequest request) {
        Object wResult;
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            String wAsnType = StringUtils.parseString(request.getParameter("AsnType"));
            String wCode = StringUtils.parseString(request.getParameter("Code"));
            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"));
            int wLineID = StringUtils.parseInt(request.getParameter("LineID"));
            int wCustomerID = StringUtils.parseInt(request.getParameter("CustomerID"));
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            String wMaterial = StringUtils.parseString(request.getParameter("Material"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
            int wStatus = StringUtils.parseInt(request.getParameter("Status"));

            ServiceResult<List<MESMaterialReturn>> wServiceResult = wWMSService
                    .MES_QueryMaterialReturnListEmployeeAllWeb(wLoginUser, wAsnType, wCode, wProductID, wLineID,
                            wCustomerID, wOrderID, wPartID, wMaterial, wStartTime, wEndTime, wStatus);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 获取待办、已办、发起 任务
     */
    @GetMapping("/EmployeeAllNew")
    public Object EmployeeAllNew(HttpServletRequest request) {
        Object wResult;
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
            int wStatus = StringUtils.parseInt(request.getParameter("Status"));

            ServiceResult<List<MESMaterialReturn>> wServiceResult =
                    wWMSService.WMS_QueryMaterialReturnEmployeeAllNew(wLoginUser,
                            wStartTime, wEndTime, wStatus);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 显示二维码
     */
    @GetMapping("/ShowQRCode")
    public Object ShowQRCode(HttpServletRequest request) {
        Object wResult;
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wReturnTaskID = StringUtils.parseInt(request.getParameter("ReturnTaskID"));

            if (wReturnTaskID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<String> wServiceResult = wWMSService.WMS_ShowReturnQRCode(wLoginUser, wReturnTaskID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 扫码交接
     */
    @GetMapping("/CodeScanningHandover")
    public Object CodeScanningHandover(HttpServletRequest request) {
        Object wResult;
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            String wCode = StringUtils.parseString(request.getParameter("Code"));

            if (StringUtils.isEmpty(wCode)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wWMSService.WMS_CodeScanningHandover(wLoginUser,
                    wCode);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 手动推送
     */
    @GetMapping("/ManualPush")
    public Object ManualPush(HttpServletRequest request) {
        Object wResult;
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wReturnID = StringUtils.parseInt(request.getParameter("ReturnID"));

            if (wReturnID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wWMSService.WMS_ManualPushMaterialReturn(wLoginUser,
                    wReturnID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 导出打印文件
     */
    @GetMapping("/ExportPrintFile")
    public Object ExportPrintFile(HttpServletRequest request) {
        Object wResult;
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wReturnID = StringUtils.parseInt(request.getParameter("ReturnID"));

            if (wReturnID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<String> wServiceResult = wWMSService.WMS_ExportPrintFile(wLoginUser,
                    wReturnID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询工艺变更待办、已办
     */
    @GetMapping("/QueryTechMessageList")
    public Object QueryTechMessageList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 1待办 2已办(已办必须给时间段)
            int wTagType = StringUtils.parseInt(request.getParameter("TagType"));
            int wModuleID = StringUtils.parseInt(request.getParameter("ModuleID"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            Calendar wBaseTime = Calendar.getInstance();
            wBaseTime.set(2010, 0, 1, 0, 0, 0);

            if (wModuleID <= 0 || (wTagType != 1 && wTagType != 2)
                    || (wTagType == 2 && wStartTime.compareTo(wBaseTime) < 0)
                    || (wTagType == 2 && wEndTime.compareTo(wBaseTime) < 0) || wStartTime.compareTo(wEndTime) > 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<BFCMessage>> wServiceResult =
                    wMyHelperService.WMS_QueryTechMessageList(wLoginUser, wTagType, wModuleID, wStartTime, wEndTime);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
